﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace translate
{
    public partial class ImportWindow : Form
    {
        class CellCls
        {
            public string Word = "";
            public string Translate = "";
            public string Complete = "";
            public string File = "";
            public string Line = "";
            public string FilePath = "";
        }
        private string _data = "";
        private List<CellCls> translateWords = new List<CellCls>();
        public ImportWindow()
        {
            InitializeComponent();
        }
        protected override void OnClosing(CancelEventArgs e)
        {
           /* e.Cancel = true;*/
            EventCenter.Broadcast(EGameEvent.eBackMainWindow);

        }
        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "文件(*.xlsx)|*.xlsx";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;
                System.Data.DataTable dataTable = ExcelOperate.ImportFromExcel(file);
                this.BeginInvoke(new System.Threading.ThreadStart(delegate ()
                {
                    ShowDataInListView(dataTable, listView);                    
                }));
            }
        }

        public void ShowDataInListView(DataTable dt, ListView lv)
        {
            translateWords.Clear();
            lv.Clear();
            lv.AllowColumnReorder = true;//用户可以调整列的位置
            lv.GridLines = true;//显示行与行之间的分隔线    
            lv.FullRowSelect = true;//要选择就是一行    
            lv.View = View.Details;//定义列表显示的方式   
            lv.Scrollable = true;//需要时候显示滚动条   
            lv.MultiSelect = false; // 不可以多行选择    
            lv.HeaderStyle = ColumnHeaderStyle.Clickable;
            lv.View = View.Details;
            int RowCount, ColumnCount;
            DataRow dr = null;
            if (dt == null) return;
            RowCount = dt.Rows.Count;
            ColumnCount = dt.Columns.Count;
            //添加列标题名
            for (int i = 0; i < ColumnCount; i++)
            {
                lv.Columns.Add(dt.Columns[i].Caption.Trim(), lv.Width / ColumnCount);
            }
            if (RowCount == 0) return;
            for (int i = 0; i < RowCount; i++)
            {
                dr = dt.Rows[i];
                lv.Items.Add(dr[0].ToString());
                CellCls cls = new CellCls();
                for (int j = 0; j < ColumnCount; j++)
                {
                    if(j != 0)
                        lv.Items[i].SubItems.Add(dr[j].ToString());
                    switch(j)
                    {
                        case 0:
                            cls.Word = dr.ItemArray[0].ToString();
                            break;
                        case 1:
                            cls.Translate = dr.ItemArray[1].ToString();
                            break;
                        case 2:
                            cls.Complete = dr.ItemArray[2].ToString();
                            break;
                        case 3:
                            cls.File = dr.ItemArray[3].ToString();
                            break;
                        case 4:
                            cls.Line = dr.ItemArray[4].ToString();
                            break;
                        case 5:
                            cls.FilePath = dr.ItemArray[5].ToString();
                            break;
                    }
                }
                translateWords.Add(cls);
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure ？", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                ImportToExcel();
            }

        }
        private void ReadFile(string filepath,int linePos,string word,string translateWord)
        {
            _data = "";
            using (StreamReader sr = new StreamReader(filepath))
            {
                string line;
                int pos = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    
                    if (linePos == pos)
                    {
                        if(line.Contains(word))
                        {
                            line = line.Replace(word, translateWord);
                        }
                        
                    }
                    _data += line + "\n";
                    pos++;
                  
                }
                sr.Close();
                sr.Dispose();
                Write(filepath, _data);
            }
        }

        public void Write(string filePath,string txt)
        {
            File.WriteAllText(filePath, txt, Encoding.UTF8);
            //             FileStream fs = new FileStream(filePath, FileMode.Create);
            //             //获得字节数组
            //             byte[] data = System.Text.Encoding.Default.GetBytes(txt);
            //             //开始写入
            //             fs.Write(data, 0, data.Length);
            //             //清空缓冲区、关闭流
            //             fs.Flush();
            //             fs.Close();
        }
        private void ImportToExcel()
        {
            foreach(var v in translateWords)
            {
                ReadFile(v.FilePath, int.Parse(v.Line), v.Word, v.Translate);
            }
        }
    }
}
